<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;

class TestController extends Controller
{

    private $where;
    private $start_date;
    private $end_date;
    public function __construct()
    {
        parent::__construct(Request::instance());
        $this->start_date = date('Y-m-d 18:00',time());
        $this->end_date = date('Y-m-d 00:00',time());
        $this->where['created_at'] = array(array('gt',$this->end_date),array('lt',$this->start_date));
    }


    public function index()
    {
        return $this->fetch('index');

    }


    public function baidus(){
        $movie = model('Movie');
        $data = $movie->limit(0,1)->select();
        foreach ($data as $v){
            if($v['stars_id']){
                $url = "http://api.douban.com/v2/movie/celebrity/1054395";
            }
        }
        var_dump($data);
    }

    //统计操作发放token
    public function tokens(){
        //查询用户的钱包地址
        $this->getWallet();
        //统计每天注册的用户
        $this->getRegister();
        //统计短评的用户
        //$this->getReview(1);
        //统计长评的用户
        //$this->getReview(2);
        //每天登陆的用户
        //$this->getLogin();
        //每天点赞的用户
        //$this->getLikes();
        //每天评论的用户
        //$this->getComments();
        //每天上传票根
        //$this->getTicket();
    }

    //每天注册的用户
    private function getRegister(){
        $result=Db::name('user')->where($this->where)->field('id')->select();
        if($result){
            $ids = array_column($result,'id');
            //存入集合中
            foreach ($ids as $v){
//                $value = (string)$v;
//                $this->redis->zAdd('films',888,$v);
                $this->changeToken($v,888,1,2);
            }
        }
    }

    //获取用户设置的钱包地址
    private function getWallet(){
        $map['ue.wallet'] = array('neq',' ');
        $result=Db::name('user')->alias('u')->join('user_extend ue','u.phone = ue.phone')->where($map)->field('u.id,ue.wallet')->select();
        //存入钱包地址无序集合中
        foreach ($result as $v){
            $this->redis->hSet('films',$v['id'],$v['wallet']);
            //$this->redis->hGet('films',$v['id']);
        }
    }

    //每天发布影评的用户
    private function getReview($type){
        $this->where['type']=$type;
        $result=Db::name('film_review')->where($this->where)->field('user_id')->select();
        if($result){
            $ids = array_column($result,'user_id');
            //1 短评 2 长评
            if($type==1){
                $score = 975000/$ids;
            }else{
                $score = 525000/$ids;
            }
            $types = $type==1?5:6;
            foreach ($ids as $v){
                $this->changeToken($v,$score,1,$types);
            }
        }

    }

    //每天影人认证的用户
    private function getFigure($type){
        $this->where['type']=$type;
        $result=Db::name('figure')->where($this->where)->where(['status'=>1])->field('user_id')->select();
        foreach ($result as $v){
            $this->changeToken($v,8888,1,0);
        }
    }

    //每天点赞的用户
    private function getLikes(){
        $result=Db::name('likes')->where($this->where)->field('user_id')->select();
        if($result){
            $ids = array_column($result,'user_id');
            $num = count($ids);
            $score = 150000/$num;
            //存入集合中
            foreach ($ids as $v){
                $this->changeToken($v,$score,1,4);
            }
        }
    }

    //每天转发的用户
    private function getorwarding(){
        $result=Db::name('forwarding')->where($this->where)->field('user_id')->select();
        if($result){
            $ids = array_column($result,'user_id');
            $num = count($ids);
            $score = 750000/$num;
            //存入集合中
            foreach ($ids as $v){
                $this->changeToken($v,$score,1,3);
            }
        }
    }

    //每天评论的用户
    private function getComments(){
        $result=Db::name('comments')->where($this->where)->field('user_id')->select();
        if($result){
            $ids = array_column($result,'user_id');
            $num = count($ids);
            $score = 600000/$num;
            //存入集合中
            foreach ($ids as $v){
                $this->changeToken($v,$score,1,2);
            }
        }
    }


    //每天上传票根的用户
    private function getTicket(){
        $result=Db::name('images_infos')->where($this->where)->field('user_id')->select();
        var_dump($result);
    }

    //添加token记录
    private function changeToken($user_id,$token,$status,$type){
        $time = date('Y-m-d H:i:s',time());
        $token_info=Db::name('user_token')->where(['user_id'=>$user_id])->find();
        if(!$token_info){
            //添加记录
            Db::name('user_token')->insert(['user_id'=>$user_id,'token'=>$token,'created_at'=>$time]);
            Db::name('token')->insert(['user_id'=>$user_id,'created_at'=>$time,'before_token'=>0,'change_token'=>$token,'after_token'=>$token,'type'=>$type,'status'=>$status]);
        }else{
            //更新记录
            $before = $token_info['token'];
            if($status==1){
                $after = $before+$token;
            }else{
                $after = $before-$token;
            }
            Db::name('user_token')->where(['user_id'=>$user_id])->update(['token'=>$after,'created_at'=>$time]);
            Db::name('token')->insert(['user_id'=>$user_id,'type'=>$type,'created_at'=>$time,'status'=>$status,'before_token'=>$before,'change_token'=>$token,'after_token'=>$after]);
        }
        //写入数据表中
        $wallet = $this->redis->hGet('films',$user_id);
        if($wallet){
            Db::name('token_wallet')->insert(['user_id'=>$user_id,'wallet'=>$wallet,'token'=>$token,'created_at'=>$time]);
        }
    }

    public function tokenList(){

        $page = input('post.page');
        $arr=wechat_page($page,'20');
        $offset=$arr['offset'];
        $length=$arr['length'];
        $re=Db::name('token_wallet')->where($this->where)->field('id,wallet,token,created_at')->limit($offset,$length)->select();
        if($re){
            respon(200,$re);
        }
        respon(201,'暂无数据！');
    }
}